<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Meta extractor
 * 
 * Extract metadata from a URL.
 */
class metaextractor_MetaExtractor
{
	protected $config = null;
	protected $urlContentFetcher = null;
	
	/**
	 * Create a new MetaExtractor instance.
	 * @param array $config Meta extractor config
	 */
	public function __construct($config)
	{
		$this->config = $config;
		$this->urlContentFetcher = new metaextractor_UrlContentFetcher($this->config);
	}

	/**
	 * Extract metadata using the configured metadata extractors.
	 * @param string $url URL
	 * @return array Array of metaextractor_Extractor objects containing the extracted metadata
	 * @throws metaextractor_exception_Exception
	 */
	public function fetchUrlMetadata($url)
	{
		$resultsByExtactorId = array();
		
		// loop over all configured metadata extractors
		foreach($this->config['extractors'] as $extractorClassName)
		{
			/* @var $extractor metaextractor_Extractor */
			$extractor = new $extractorClassName();
			
			// check if the extractor supports the current URL
			if($extractor->getSupportedUrlPattern() === null || preg_match($extractor->getSupportedUrlPattern(), $url))
			{
				$results = $extractor->processUrl($url, $this->urlContentFetcher);

				if(count($results) > 0)
				{
					$resultsByExtactorId[$extractorClassName] = $results;
					
					// stop if multimode is disabled
					if(!$this->config['multimode'])
					{
						break;
					}
				}
			}
		}
		
		return $resultsByExtactorId;
	}
}